<template>
    <div id="comboList">
        <div>
            <el-button type="success" @click="addClick" icon="el-icon-plus" style="margin: 10px">新增</el-button>
        </div>
        <!--表格-->
        <el-table :data="tableData" border max-height="500" height="500">
            <el-table-column prop="name" label="角色名称" align="center"/>
            <el-table-column prop="des" label="角色描述" align="center"/>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button :type="scope.row.status==1?'success':'danger'" size="mini" @click="delClick(scope.row)">
                        删除
                    </el-button>
                    <el-button @click="editClick(scope.row)" type="primary" size="mini">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--翻页-->
        <div>
            <pageHelper :current-page="currentPage" :total="total" @sizeChange="handleSizeChange"
                        @currentChange="handleCurrentChange" class="my_pagenation"/>
        </div>
        <!--新增表单-->
        <el-dialog title="新增" :visible.sync="addDialogFormVisible" width="40%">
            <el-form :model="addForm">
                <el-form-item label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="addForm.name" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="角色描述" :label-width="formLabelWidth">
                    <el-input v-model="addForm.edit" autocomplete="off"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addCancel">取 消</el-button>
                <el-button type="primary" @click="addConfirm">确 定</el-button>
            </div>
        </el-dialog>
        <!--编辑表单-->
        <el-dialog title="编辑" :visible.sync="editDialogFormVisible" @close="editFormClose">
            <el-form :model="editForm">
                <el-form-item label="用户角色" :label-width="formLabelWidth">
                    <el-input v-model="editForm.channelName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="用户描述" :label-width="formLabelWidth">
                    <el-input v-model="editForm.vendor" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="权限分配" :label-width="formLabelWidth">
                    <el-tree
                        :data="data3"
                        show-checkbox
                        node-key="id"
                        :default-expand-all="true"
                        :props="defaultProps"
                        style="margin: 7px;" ref="nodeTree">
                    </el-tree>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editConfirm">确 定</el-button>
            </div>
        </el-dialog>
        <loading v-if="loading"></loading>
    </div>
</template>

<script>
    import axios from 'axios';
    import pageHelper from '@/website/center/common/pageHelper';
    import {timeFormat} from '@/module/tools';
    import {getMonthStartDate} from '@/module/dateUtil';
    import {Loading} from 'element-ui';

    function node123(id) {
        this.label = '系统管理';
        this.id = id;
        this.children = [];
    }

    export default {
        name: 'roleList',
        components: {pageHelper, Loading},
        data() {
            return {
                columnWidth: 100,
                currentPage: 2,
                total: 120,
                type: "danger",
                loading: false,
                loadInstance: null,
                addDialogFormVisible: false,
                editDialogFormVisible: false,
                formLabelWidth: '80px',
                tableData: [{
                    name: '运营',
                    des: 'test',
                }],
                searchFormData: {
                    createTime: [],
                    channel: '',
                    combo: '',
                },
                channels: [{label: "通道1", value: 1},
                    {label: "通道2", value: 2},
                    {label: "通道3", value: 3}],
                combos: [{label: "套餐1", value: 1},
                    {label: "套餐2", value: 2},
                    {label: "套餐3", value: 3}],
                apiAddress: [
                    {label: "地址1", value: "url1"},
                    {label: "地址2", value: "url2"},
                    {label: "地址3", value: "url3"}],
                addForm: {
                    name: '',
                    edit: '',

                },
                editForm: {
                    channelName: '',
                    vendor: '',
                    username: '',
                    password: '',
                    key: '',
                    url: ''
                },
                options: '',
                data3: [],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                checkStrictly: true,
            }
        },
        methods: {
            delClick(row) {
                let status = row.status;
                if (status == 1) {
                    status = "启用";
                } else if (status == 0) {
                    status = "停用";
                }
                this.$confirm('此操作将' + status + '该套餐, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let loading = this.$loading(this.options);
                    this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
                        setTimeout(() => {
                            loading.close();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }, 2000);
                    });
                }).catch(() => {
                    /*  this.$message({
                          type: 'info',
                          message: '已取消删除'
                      });*/
                    console.log("取消")
                });
            }
            ,
            searchSubmit() {
                console.log('查询提交====');
                console.log(this.searchFormData);
                let beginTime = this.searchFormData.createTime[0];
                let endTime = this.searchFormData.createTime[1];
                let str = timeFormat(beginTime, "yyyy-MM-dd");
                console.log("打印日期", str);
                let b = getMonthStartDate();
                console.log(b);
            }
            ,
            addClick() {
                this.addDialogFormVisible = true;
            }
            ,
            //确认新增
            addConfirm() {
                console.log("获取新增表的数据");
                console.log(this.addForm);
            }
            ,
            addCancel() {
                this.addDialogFormVisible = false;
                //注意关闭数据后的数据清楚
            }
            ,
            editClick(row) {
                //构造树形控件的节点
                let n = 8;
                let m = 8;
                for (let j = 1; j < n; j++) {
                    let node = new node123(j);
                    for (let k = 1; k < 3; k++) {
                        node.children.push(new node123(m++));
                    }
                    this.$data.data3.push(node);
                }
                console.log('==编辑操作==');
                console.log(row);
                console.log('data3', this.data3);
                this.editDialogFormVisible = true;
                //加载完毕后才操作节点
                this.$nextTick(() => {
                    console.log('树结点', this.$refs.nodeTree);
                    this.$refs.nodeTree.setChecked(1, true, false);
                });
            }
            ,
            //确认修改
            editConfirm(row) {
                //子节点选了，父节点也会勾选
                let nodes = this.$refs.nodeTree.getCheckedNodes(false, true);
                console.log('编辑确认...', this.$refs.nodeTree);
                for (let i = 0; i < nodes.length; i++) {
                    console.log(nodes[i].id);
                }
            },
            editFormClose() {
                this.editForm = {
                    channelName: '',
                    vendor: '',
                    username: '',
                    password: '',
                    key: '',
                    url: ''
                }
                this.data3 = []
            }
            ,
            handleSizeChange(val) {
                console.log(`123每页 ${val} 条`);
            }
            ,
            handleCurrentChange(val) {
                console.log(`345当前页: ${val}`);
            }
        }
        ,
        mounted() {
            this.options = {
                text: '加载中...',
                background: 'rgba(0,0,0,0,7)',
                target: document.getElementById("comboList")
            }
            let createTime = this.$data.searchFormData.createTime;
            createTime.push(getMonthStartDate());
            createTime.push(Date());
            console.log(this.$data);
            let obj = {
                name: '经理',
                des: '12132',
            };
            let i = 0;
            for (i = 0; i < 4; i++) {
                console.log("添加对象")
                this.$data.tableData.push(obj);
            }
            console.log("data3", this.$data.data3)

        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
    .express-ctr-item {
        margin-top: 12px;
    }

    .express-ctr-item .express-ctr-label {
        display: inline-block;
        width: 100px;
        text-align: right;
        line-height: 32px;
    }

    /*使图表中的文字居中*/
    /*  .el-table .cell {
          box-sizing: border-box;
          white-space: normal;
          word-break: break-all;
          line-height: 23px;
          text-align: center;
      }*/
    .my-input {
        width: 40%;
    }

</style>

